﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace Nivdal.TBP
{
    public class ForumConvert
    {
        public string ForumConvertString(Object toParserString)
        {
            //System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string Converted = String.Empty;
            //string TestString = "";
            string p = toParserString.ToString();
            string TempString = String.Empty;
            string PlayerString = String.Empty;
            string TeamString = String.Empty;
            string LinkString = String.Empty;
            string CoachString = String.Empty;
            string MatchString = String.Empty;
            string CountryString = String.Empty;
            string StadiumString = String.Empty;
            string YouTubeString = String.Empty;
            ArrayList TempAL = new ArrayList();

            Stack Tags = new Stack();

            for (int i = 0; i < p.Length; i++)
            {
                if (p[i].CompareTo('[') == 0)
                {
                    while (p[i].CompareTo(']') != 0 )  //find the TAG
                    {
                        TempAL.Add(p[i]);
                        ++i;
                    }

                    TempAL.Add(p[i]);//get the last char
                    char[] TempChar = (char[])TempAL.ToArray(typeof(char));
                    TempString = new string(TempChar); //TempString contain the TAG
                    Array.Clear(TempChar, 0, TempChar.Length);
                    TempAL.Clear();
                    switch (TempString)
                    {
                        case "[B]":
                            Converted += ("<b>");
                            break;
                        case "[/B]":
                            Converted += ("</b>");
                            break;
                        case "[U]":
                            Converted += ("<u>");
                            break;
                        case "[/U]":
                            Converted += ("</u>");
                            break;
                        case "[I]":
                            Converted += ("<i>");
                            break;
                        case "[/I]":
                            Converted += ("</i>");
                            break;
                        case "[QUOTE]":
                            Converted += ("<div class=\"quoteclass\">");
                            break;
                        case "[/QUOTE]":
                            Converted += ("</div>");
                            break;
                        case "[PIC]":
                            Converted += ("<img src=\"");
                            break;
                        case "[/PIC]":
                            Converted += ("\" width=\"100\" height=\"100\"/>");
                            break;
                        case "[PLAYER]":
                            Converted += ("<a href=Player.aspx?Id=");
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the player id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempPlayerId = (char[])TempAL.ToArray(typeof(char));
                            PlayerString = new string(TempPlayerId); //TempString contain the the playerid
                            Array.Clear(TempPlayerId, 0, TempPlayerId.Length);
                            TempAL.Clear();
                            break;
                        case "[/PLAYER]":
                            Converted += "  class=\"internalLinkStyle\">";
                            Converted += PlayerString;
                            Converted += "</a>";
                            break;
                        case "[LINK]":
                            Converted += ("<a href=");
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the link id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempLinkString = (char[])TempAL.ToArray(typeof(char));
                            LinkString = new string(TempLinkString); //TempString contain the the linkid
                            Array.Clear(TempLinkString, 0, TempLinkString.Length);
                            TempAL.Clear();
                            break;
                        case "[/LINK]":
                            Converted += " " + LinkString;
                            Converted += "  class=\"internalLinkStyle\" rel=\"nofollow\">";
                            Converted += LinkString;
                            Converted += "</a>";
                            break;
                        case "[TEAM]":
                            Converted += ("<a href=TeamHomePage.aspx?TeamId=");
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the player id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempTeamId = (char[])TempAL.ToArray(typeof(char));
                            TeamString = new string(TempTeamId); //TempString contain the the teamid
                            Array.Clear(TempTeamId, 0, TempTeamId.Length);
                            TempAL.Clear();
                            break;
                        case "[/TEAM]":
                            Converted += "  class=\"internalLinkStyle\">";
                            Converted += TeamString;
                            Converted += "</a>";
                            break;
                        case "[COACH]":
                            Converted += ("<a href=coach.aspx?Id=");
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the Coach id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempCoachId = (char[])TempAL.ToArray(typeof(char));
                            CoachString = new string(TempCoachId); //TempString contain the the teamid
                            Array.Clear(TempCoachId, 0, TempCoachId.Length);
                            TempAL.Clear();
                            break;
                        case "[/COACH]":
                            Converted += "  class=\"internalLinkStyle\">";
                            Converted += CoachString;
                            Converted += "</a>";
                            break;
                        case "[MATCH]":
                            Converted += ("<a href=MatchPage.aspx?MatchId=");
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the Coach id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempMatchId = (char[])TempAL.ToArray(typeof(char));
                            MatchString = new string(TempMatchId); //TempString contain the the teamid
                            Array.Clear(TempMatchId, 0, TempMatchId.Length);
                            TempAL.Clear();
                            break;
                        case "[/MATCH]":
                            Converted += "  class=\"internalLinkStyle\">";
                            Converted += MatchString;
                            Converted += "</a>";
                            break;
                        case "[COUNTRY]":
                            Converted += ("<a href=Country.aspx?Id=");
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the Coach id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempCountryId = (char[])TempAL.ToArray(typeof(char));
                            CountryString = new string(TempCountryId); //TempString contain the the teamid
                            Array.Clear(TempCountryId, 0, TempCountryId.Length);
                            TempAL.Clear();
                            break;
                        case "[/COUNTRY]":
                            Converted += "  class=\"internalLinkStyle\">";
                            Converted += CountryString;
                            Converted += "</a>";
                            break;
                        case "[STADIUM]":
                            Converted += ("<a href=StadiumPage.aspx?StadiumId=");
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the Coach id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempStadiumId = (char[])TempAL.ToArray(typeof(char));
                            StadiumString = new string(TempStadiumId); //TempString contain the the teamid
                            Array.Clear(TempStadiumId, 0, TempStadiumId.Length);
                            TempAL.Clear();
                            break;
                        case "[/STADIUM]":
                            Converted += "  class=\"internalLinkStyle\">";
                            Converted += StadiumString;
                            Converted += "</a>";
                            break;
                        case "[YOUTUBE]":
                            Converted += "<object width=425 height=349><param name=movie value=http://www.youtube-nocookie.com/v/A8pVO13-xjQ?version=3&amp;hl=en_US&amp;rel=0></param><param name=allowFullScreen value=true></param><param name=allowscriptaccess value=always></param><embed src=http://www.youtube-nocookie.com/v/";
                            for (int j = i + 1; p[j].CompareTo('[') != 0; j++)//take the Coach id
                            {
                                TempAL.Add(p[j]);
                            }
                            char[] TempYouTubeId = (char[])TempAL.ToArray(typeof(char));
                            YouTubeString = new string(TempYouTubeId); //TempString contain the the teamid
                            Array.Clear(TempYouTubeId, 0, TempYouTubeId.Length);
                            TempAL.Clear();
                            break;
                        case "[/YOUTUBE]":
                            Converted += " " + YouTubeString;
                            Converted += "?version=3&amp;hl=en_US&amp;rel=0 type=application/x-shockwave-flash width=425 height=349 allowscriptaccess=always allowfullscreen=true></embed></object>";
                            //Converted += YouTubeString;
                            //Converted += "</a>";
                            break;
                        default:
                            Console.WriteLine("Invalid selection.");
                            break;
                    }
                }

                if (i < p.Length && p[i].CompareTo(']') != 0)
                {
                    Converted += p[i];
                    //if (p[i].Equals('\r') || p[i].Equals('\n'))
                    //    Converted += "<br /> "; //catch the Carriage return

                }
            };
            //TestString = Converted.ToString();
            return Converted.Replace("\r\n", "<br /> ");
        }
    }
}